package com.jiuzhou.surery.Controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jiuzhou.surery.beans.Item;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Controller
public class AddItemController {
    
    @RequestMapping("/add")
    public String add(Model model){
        model.addAttribute("item", new Item());
        return "add";
    }

    @PostMapping("/add")
    public String add(@ModelAttribute Item item, Model model){
        model.addAttribute("item", item);
        // 添加当前时间到模型
        addCurrentTimeToModel(model);
        return "result";
    }

  
    // 辅助方法：获取当前本地时间并添加到模型
    private void addCurrentTimeToModel(Model model) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedTime = now.format(formatter);
        model.addAttribute("currentTime", formattedTime);
    }
}